package com.smooth.site.sx.pc.job.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.smooth.common.pc.interceptor.PcAccountContext;
import com.smooth.common.site.common.enums.SiteCodeEnums;
import com.smooth.common.site.common.excep.SiteException;
import com.smooth.common.site.common.rsp.ReturnResult;
import com.smooth.common.site.common.utils.IdGenerater;
import com.smooth.common.site.common.utils.StringUtils;
import com.smooth.dao.gen.BizJobMapper;
import com.smooth.dao.gen.bean.BizCompany;
import com.smooth.dao.gen.bean.BizJob;
import com.smooth.dao.gen.bean.BizStudent;
import com.smooth.site.sx.mag.biz.comp.service.BizCompanyService;
import com.smooth.site.sx.mag.biz.job.service.BizJobService;
import com.smooth.site.sx.pc.job.req.invite.InviteDetailReq;
import com.smooth.site.sx.pc.job.req.invite.InvitePubUpload;
import com.smooth.site.sx.pc.job.req.invite.InviteQueryReq;
import com.smooth.site.sx.pc.job.req.invite.InviteReq;
import com.smooth.site.sx.pc.job.req.invite.StuInviteQueryReq;
import com.smooth.site.sx.pc.job.service.PcJobService;
import com.smooth.site.sx.pc.student.service.PcStudentService;
import com.smooth.sys.pc.vo.ListRsp;
import com.smooth.sys.site.biz.member.gen.service.BizMemberService;
import com.smooth.sys.site.biz.sms.bx.service.SmsCodeService;

@Controller
@RequestMapping(value = "/pc")
public class PcJobController {
	@Autowired
	BizMemberService bizMemberService;
	@Autowired
	SmsCodeService smsCodeService;
	@Autowired
	IdGenerater idGenerater;
	@Autowired
	PcStudentService pcStudentService;
	@Autowired
	PcJobService pcJobService;

	@RequestMapping(value = {"c/comp/pubjob"},method = {RequestMethod.POST,RequestMethod.GET})
	@ResponseBody
	public ReturnResult register(@Valid InviteReq inviteReq, BindingResult result,HttpServletRequest request, HttpServletResponse response) {
		ReturnResult returnResult = new ReturnResult();
		BizCompany secBizCompany = (BizCompany)PcAccountContext.getObj(BizCompany.class);
//		if(secBizCompany.getPercent()<1) {
//			throw new SiteException(SiteCodeEnums.COMP_NOTCOMPLETE, secBizCompany.getCompName()+ "");
//		}
		BizJob bizJob = pcJobService.addJob(inviteReq, secBizCompany);
		returnResult.setData(bizJob);
		return returnResult;
	}
	
	
	@RequestMapping(value = {"c/comp/queryjob"},method = {RequestMethod.POST,RequestMethod.GET})
	@ResponseBody
	public ReturnResult queryjob(@Valid InviteQueryReq inviteQueryReq,BindingResult result,HttpServletRequest request, HttpServletResponse response) {
		ReturnResult returnResult = new ReturnResult();
		BizCompany secBizCompany = (BizCompany)PcAccountContext.getObj(BizCompany.class);
		
		ListRsp<BizJob> brsp = pcJobService.queryJob(secBizCompany, inviteQueryReq);
		returnResult.setData(brsp);
		return returnResult;
	}
	
	@RequestMapping(value = {"c/comp/jobdetail"},method = {RequestMethod.POST,RequestMethod.GET})
	@ResponseBody
	public ReturnResult jobdetail(@Valid InviteDetailReq inviteDetailReq,BindingResult result,HttpServletRequest request, HttpServletResponse response) {
		ReturnResult returnResult = new ReturnResult();
		BizCompany secBizCompany = (BizCompany)PcAccountContext.getObj(BizCompany.class);
		
		BizJob bizJob = pcJobService.queryJobDetail(secBizCompany, inviteDetailReq);
		returnResult.setData("bizJob", bizJob);
		returnResult.setData("comp", secBizCompany);
		return returnResult;
	}
	
	
	
	@RequestMapping(value = {"c/comp/updjob"},method = {RequestMethod.POST,RequestMethod.GET})
	@ResponseBody
	public ReturnResult updjob(@Valid InviteReq inviteReq,BindingResult result,HttpServletRequest request, HttpServletResponse response) {
		ReturnResult returnResult = new ReturnResult();
		BizCompany secBizCompany = (BizCompany)PcAccountContext.getObj(BizCompany.class);
		if(StringUtils.isBlank(inviteReq.getId())) {
			inviteReq.setId(inviteReq.getJobId());
		}
		if(StringUtils.isBlank(inviteReq.getId())) {
			throw new SiteException(SiteCodeEnums.param_error," 更新职位缺少职位ID");
		}
		BizJob bizJob =  pcJobService.addJob(inviteReq,secBizCompany);
		returnResult.setData(bizJob);
		return returnResult;
	}
	
	
	@RequestMapping(value = {"c/comp/upstatus"},method = {RequestMethod.POST,RequestMethod.GET})
	@ResponseBody
	public ReturnResult upstatus(@Valid InvitePubUpload invitePubUpload,BindingResult result,HttpServletRequest request, HttpServletResponse response) {
		ReturnResult returnResult = new ReturnResult();
		BizCompany secBizCompany = (BizCompany)PcAccountContext.getObj(BizCompany.class);
		if(StringUtils.isBlank(invitePubUpload.getId())) {
			invitePubUpload.setId(invitePubUpload.getJobId());
		}
		if(StringUtils.isBlank(invitePubUpload.getId())) {
			throw new SiteException(SiteCodeEnums.param_error," 更新职位缺少职位ID");
		}
		BizJob bizJob =  pcJobService.updJobStatus(invitePubUpload,secBizCompany);
		returnResult.setData(bizJob);
		return returnResult;
	}
	
	
	@Autowired
	BizJobMapper bizJobMapper;
	@Autowired
	BizCompanyService bizCompanyService;
	@RequestMapping(value = {"p/stu/queryjob"},method = {RequestMethod.POST,RequestMethod.GET})
	@ResponseBody
	public ReturnResult stuQueryjob(@Valid StuInviteQueryReq stuInviteQueryReq,BindingResult result,HttpServletRequest request, HttpServletResponse response) {
		ReturnResult returnResult = new ReturnResult();
		BizStudent bizStudent = (BizStudent)PcAccountContext.getObj(BizStudent.class);
		ListRsp<BizJob> brsp = pcJobService.queryStuJobs(stuInviteQueryReq,bizStudent);
		returnResult.setData(brsp);
		return returnResult;
	}
	@Autowired
	BizJobService bizJobService;
	
	@RequestMapping(value = {"p/stu/jobdetail"},method = {RequestMethod.POST,RequestMethod.GET})
	@ResponseBody
	public ReturnResult stuJobdetail(@Valid InviteDetailReq inviteDetailReq,BindingResult result,HttpServletRequest request, HttpServletResponse response) {
		ReturnResult returnResult = new ReturnResult();
		BizJob bizJob = bizJobService.get(inviteDetailReq.getJobId());
		if(bizJob!=null) {
			PcJobService.setBlackKeyInfo(bizJob);
		}
		returnResult.setData("bizJob", bizJob);
		returnResult.setData("bizComp", bizCompanyService.get(bizJob.getCompId()));
		return returnResult;
	}

}
